﻿#ifndef QNETWORKTRAFFICMONITOR_H
#define QNETWORKTRAFFICMONITOR_H

#include <QVector>
#include <QString>
#include <QElapsedTimer>

// 结构体需要新增速率计算专用字段
struct NetworkInterfaceTraffic {
    QString name;
    QString description;
    QString macAddress;
    bool isUp;
    quint64 bytesReceived;       // 64位累计接收值（显示用）
    quint64 bytesSent;           // 64位累计发送值（显示用）
    quint64 packetsReceived;
    quint64 packetsSent;
    double downloadRate;         // 接收速率
    double uploadRate;           // 发送速率
    qint64 lastUpdateTime;       // 最后更新时间戳
    quint32 rawReceived;         // 32位原始接收值
    quint32 rawSent;             // 32位原始发送值
    quint64 prevReceived;        // 当前64位累计接收值
    quint64 prevSent;            // 当前64位累计发送值
    quint64 lastRateReceived;    // 上一次计算速率时的累计接收值（专用基准）
    quint64 lastRateSent;        // 上一次计算速率时的累计发送值（专用基准）
};

struct TotalNetworkTraffic {
    quint64 totalBytesReceived;    // 总接收字节数
    quint64 totalBytesSent;        // 总发送字节数
    double totalDownloadRate;      // 总接收速率
    double totalUploadRate;        // 总发送速率
};

class QNetworkTrafficMonitor {
public:
    QNetworkTrafficMonitor();
    QVector<NetworkInterfaceTraffic> getInterfaceTraffics();
    TotalNetworkTraffic getTotalTraffic();

private:
    QVector<NetworkInterfaceTraffic> getInterfaceTrafficsImpl();
    void calculateRates(QVector<NetworkInterfaceTraffic>& current);

    QVector<NetworkInterfaceTraffic> m_prevTraffics;
    QElapsedTimer m_timer;
};

#endif // QNETWORKTRAFFICMONITOR_H
